import main from "@/assets/js/public.js"; //公用js
import getInfo from "@/assets/js/getInfo.js";
import router from '../../router/index';
import $ajax from "@/assets/js/http.js";
import JSEncrypt from "@/assets/js/rsaSecret.js";


// 跳转微信 获取权限

export default {
  // 获取公钥加密
  setRSA(callback) {
    $ajax.http.post({
        funcName: "/fwdj/rsa/getPublicKey",
      })
      .then(res => {
        if (res && res.code == "200") {
          let RSA = JSEncrypt(res.publicKey, null);
          callback(RSA);
        }
      });
  },
  // 获取私钥解密
  setRSADeCode(callback) {
    $ajax.http.post({
        funcName: "/fwdj/rsa/getPrivateKey",
      })
      .then(res => {
        if (res && res.code == "200") {
          let RSA = JSEncrypt(null, res.privateKey);
          callback(RSA);
        }


      });
  },
  getYyAppid(data) {
    // 获取医院对应的appid
    $ajax.http.post({
      funcName: "/gzd/wechat/getYyAppid.service",
      params: {
        yljgdm: data['hosCode']
      },
    }).then((res) => {
      let data = res.data;
      this.wechatGetCode(data ? data['yyAppid'] : '');
    })
  },
  wechatGetCode(yyAppid) {
    // 获取医院对应的code
    let REDIRECT_URI = window.location.href;
    var APPID = yyAppid;
    var thisHref = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + '&redirect_uri=' + REDIRECT_URI + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
    var code = (new main.GetUrlParams()).code;
    if (yyAppid && !code) {
      window.location.href = thisHref;
    } else {

      // 有了code 调用微信支付的参数
      console.log(code);
    }
  },
  wechatLogin(callback, url) {
    var REDIRECT_URI = url ? url : main.getLocalhostPath();
    // var REDIRECT_URI = 'https://fwdj.hpwjsns.org.cn';
    // 正式
    // var APPID = "wx19000fcda6138605";
    // 测试
    var APPID = "wx4f38e1e331944941";
    var thisHref = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + '&redirect_uri=' + REDIRECT_URI + '&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect';
    var code = (new main.GetUrlParams()).code;
    if (!code && !sessionStorage.getItem("openId")) { //无code跳转微信链接获取code
      window.location.href = thisHref;
    } else {
      // 去除url上的code
      let path = window.location.pathname;
      history.replaceState({}, "", path);
      if (sessionStorage.getItem("openId")) { //已经调用过获取openid
        callback();

      } else {

        // 获取openid 
        this.getlogin(code, callback);
      }
    }
  },
  getlogin(code, callback) {
    getInfo.getLogin({
      funcName: "/fwdj/wx/getOpenId",
      params: {
        code: code
      }
    }).then(data => {
      let res = data.data;
      console.log(res);

      // if (res["code"].indexOf("EXCEPTION") == 0) { //获取openid失败
      //   main.toast(res["message"]);
      // } else if (res["code"].indexOf("ERROR") == 0) {

      if (res && res.code == "200") { //获取成功
        sessionStorage.clear();
        sessionStorage.setItem("openId", res.openId);
        callback();
      } else {
        main.toast(res["message"]);
      }

      // } else if (res["code"].indexOf("SUCCESS") == 0) { //绑定过账号 直接获取用户信息
      //   sessionStorage.clear();
      //   sessionStorage.setItem("code", code);
      //   getInfo.getUserInfo().then(data => {
      //     var jkcsUserInfo = JSON.parse(data)
      //     callback();
      //   });


      // }


    })

  }
}